package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.EquipmentArchives;
import com.hgxd.entity.vo.EquipmentArchivesVo;
import com.hgxd.result.Result;

import com.hgxd.service.IEquipmentArchivesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备档案表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "设备档案表")
@RestController
@RequestMapping("/api/equipment-archives")
@CrossOrigin
public class EquipmentArchivesController {
    @Resource(name = "EquipmentArchivesServiceImpl")
    private IEquipmentArchivesService equipmentArchivesService;

    /**
     * 得到所有的设备组织名称
     */
    @GetMapping("/getAllOrganization")
    @ApiOperation(value = "获取全部的设备组织", notes = "获取全部的设备组织")
    @OperationLogAnnotation(operModule = "设备档案表-获取全部的设备组织", operType = "获取", operContent = "获取全部的设备组织")
    public Result<List<Map<String, Object>>> getAllOrganization() {
        List<Map<String, Object>> getAllOrganization = equipmentArchivesService.getAllOrganization();
        return Result.ok(getAllOrganization);
    }

    /**
     * 根据组织得到设备
     */
    @PostMapping("/getAllEquipmentByOrganization")
    @ApiOperation(value = "根据组织得到设备", notes = "根据组织得到设备")
    @ApiImplicitParam(name = "organizationName", value = "设备组织名称")
    @OperationLogAnnotation(operModule = "设备档案表-根据组织得到设备", operType = "获取", operContent = "根据组织得到设备")
    public Result<List<EquipmentArchives>> getAllEquipmentByOrganization(@RequestParam String organizationName) {
        List<EquipmentArchives> allEquipmentByOrganization = equipmentArchivesService.getAllEquipmentByOrganization(organizationName);
        return Result.ok(allEquipmentByOrganization);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "设备档案的新增", notes = "新增设备的档案")
    @ApiImplicitParam(name = "equipmentArchives", value = "设备档案的实体类")
    @OperationLogAnnotation(operModule = "设备档案-新增", operType = "新增", operContent = "新增设备档案")
    public Result<String> insert(@RequestBody EquipmentArchives equipmentArchives) {
        boolean save = equipmentArchivesService.save(equipmentArchives);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "设备档案的更新", notes = "更新设备的档案")
    @ApiImplicitParam(name = "equipmentArchives", value = "设备档案的实体类")
    @OperationLogAnnotation(operModule = "设备档案-更新", operType = "更新", operContent = "更新设备档案")
    public Result<String> update(@RequestBody EquipmentArchives equipmentArchives) {
        boolean data = equipmentArchivesService.updateById(equipmentArchives);
        return data ? Result.ok("更新成功") : Result.fail("更新失败");
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id进行设备档案的删除", notes = "删除设备的档案")
    @ApiImplicitParam(name = "id", value = "设备档案的id")
    @OperationLogAnnotation(operModule = "设备档案-删除", operType = "删除", operContent = "删除设备档案")
    public Result<String> delete(@PathVariable Integer id) {
        boolean data = equipmentArchivesService.removeById(id);
        return data ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据id的list数组进行设备档案的删除", notes = "批量删除设备的档案")
    @ApiImplicitParam(name = "ids", value = "设备档案的id集合")
    @OperationLogAnnotation(operModule = "设备档案-批量删除", operType = "删除", operContent = "批量删除设备档案")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = equipmentArchivesService.removeByIds(ids);
        return data ? Result.ok("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "得到全部的设备档案", notes = "得到全部的设备档案")
    @OperationLogAnnotation(operModule = "设备档案-查询全部", operType = "查询全部", operContent = "查询全部设备档案")
    public Result<List<EquipmentArchives>> findAll() {
        return Result.ok(equipmentArchivesService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个设备档案", notes = "查询一个设备档案")
    @ApiImplicitParam(name = "id", value = "设备档案的id")
    @OperationLogAnnotation(operModule = "设备档案-根据id查询一个", operType = "根据id查询一个", operContent = "根据id查询一个设备档案")
    public Result<EquipmentArchives> findOne(@PathVariable Integer id) {
        EquipmentArchives byId = equipmentArchivesService.getById(id);
        if (byId == null) {
            return Result.fail();
        } else {
            return Result.ok(byId);
        }
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @ApiImplicitParam(name = "equipmentArchivesVo", value = "可以根据设备名称、所属组织、设备品牌进行模糊查询")
    @OperationLogAnnotation(operModule = "设备档案-分页模糊查询", operType = "分页模糊查询", operContent = "分页模糊查询设备档案")
    public Result<Page<EquipmentArchives>> findPage(@RequestBody EquipmentArchivesVo equipmentArchivesVo) {
        LambdaQueryWrapper<EquipmentArchives> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!"".equals(equipmentArchivesVo.getName()), EquipmentArchives::getName, equipmentArchivesVo.getName());
        lambdaQueryWrapper.eq(!"".equals(equipmentArchivesVo.getOrganizationName()), EquipmentArchives::getOrganizationName, equipmentArchivesVo.getOrganizationName());
        lambdaQueryWrapper.eq(!"".equals(equipmentArchivesVo.getManufacturers()), EquipmentArchives::getManufacturers, equipmentArchivesVo.getManufacturers());
        lambdaQueryWrapper.eq(EquipmentArchives::getIsdelete, "0");
        return Result.ok(equipmentArchivesService.page(new Page<>(equipmentArchivesVo.getPageNum(), equipmentArchivesVo.getPageSize()), lambdaQueryWrapper));
    }
}

